package com.visionet.waveletData.web.repository.group.impl;

import com.visionet.waveletData.web.vo.GroupVo;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by hxpeng on 2017/1/11.
 */
@Repository
public class GroupDaoImpl {

    private final static Logger logger = LoggerFactory.getLogger(GroupDaoImpl.class);

    @Resource
    private JdbcTemplate jdbcTemplate;

    public PageImpl<GroupVo> findPageGroup(GroupVo groupVo, Pageable page){
        StringBuffer sql = new StringBuffer();
        List<Object> params = new ArrayList<Object>();
        sql.append("select pg.ID,pg.company_id,pg.create_by,pg.create_date,pg.group_name,pg.robot_number,pg.update_by,pg.update_date,pg.is_deleted,ppp.plan_name,pg.in_plan_id from ");
        sql.append(" p_group as pg LEFT JOIN p_play_plan as ppp on pg.in_plan_id = ppp.ID where pg.is_deleted='0' ");
        sql.append(" and pg.company_id = ? ");
        params.add(groupVo.getCompanyId());
        if(StringUtils.isNotBlank(groupVo.getGroupName())){
            sql.append(" and pg.group_name like ? ");
            params.add("%"+groupVo.getGroupName()+"%");
        }
        if(StringUtils.isNotBlank(groupVo.getPlanId())){
        	/*sql.append(" and pg.id not in (select ppg.group_id from p_plan_group ppg where ppg.is_deleted='0' and ppg.plan_id= ? ");
        	params.add(groupVo.getPlanId());
        	sql.append(" ) ");*/
        	sql.append(" and pg.id not in (select ppg.group_id from p_plan_group ppg where ppg.is_deleted='0') ");
        }
        sql.append(" order by pg.create_date desc ");

        if(page == null){
            page = new PageRequest(0, 10);
        }
        int currentPage = page.getPageNumber();

        int pageSize = page.getPageSize();
        int size = jdbcTemplate.queryForInt("select count(*) from ("+sql.toString()+") as c", params.toArray());
        List<GroupVo> list = jdbcTemplate.query("select t.* from ("+sql+") as t limit  "+ currentPage * pageSize+","+pageSize, params.toArray(), new RowMapper<GroupVo>() {
            @Override
            public GroupVo mapRow(ResultSet rs, int rowNum) throws SQLException {
                GroupVo entity = new GroupVo();
                entity.setId(rs.getLong("id"));
                entity.setGroupId(String.valueOf(rs.getLong("id")));
                entity.setGroupName(rs.getString("group_name"));
                entity.setRobotNumber(rs.getInt("robot_number"));
                entity.setCompanyId(rs.getString("company_id"));
                entity.setCreateBy(rs.getString("create_by"));
                entity.setCreateDate(rs.getTimestamp("create_date"));
                entity.setUpdateBy(rs.getString("update_by"));
                entity.setUpdateDate(rs.getTimestamp("update_date"));
                entity.setIsDeleted(rs.getInt("is_deleted"));
                entity.setPlanId(rs.getString("in_plan_id"));
                entity.setPlanName(rs.getString("plan_name"));
                return entity;
            }
        });
        PageImpl<GroupVo> cmProductGroupTourPage = new PageImpl<GroupVo>(list, page, size);
        return cmProductGroupTourPage;
    }


}
